home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / commands / kermit / ckucmd.c < prev    next >
C/C++ Source or Header  |  1990-07-19  |  37KB  |  1,129 lines

  1. char *cmdv = "Unix cmd package V2(022), 27 Jan 88";
  2.  
  3. /*  C K U C M D  --  Interactive command package for Unix  */
  4.  
  5. /*
  6.  V2 adds support for Data General and Apollo Aegis.
  7. */
  8. /*
  9.  Modelled after the DECSYSTEM-20 command parser (the COMND JSYS)
  10.  
  11.  Features:
  12.  . parses and verifies keywords, text strings, numbers, and other data
  13.  . displays appropriate menu or help message when user types "?"
  14.  . does keyword and filename completion when user types ESC
  15.  . accepts any unique abbreviation for a keyword
  16.  . allows keywords to have attributes, like "invisible"
  17.  . can supply defaults for fields omitted by user
  18.  . provides command line editing (character, word, and line deletion)
  19.  . accepts input from keyboard, command files, or redirected stdin
  20.  . allows for full or half duplex operation, character or line input
  21.  . settable prompt, protected from deletion
  22.  
  23.  Functions:
  24.   cmsetp - Set prompt (cmprom is prompt string, cmerrp is error msg prefix)
  25.   cmsavp - Save current prompt
  26.   prompt - Issue prompt 
  27.   cmini  - Clear the command buffer (before parsing a new command)
  28.   cmres  - Reset command buffer pointers (before reparsing)
  29.   cmkey  - Parse a keyword
  30.   cmnum  - Parse a number
  31.   cmifi  - Parse an input file name
  32.   cmofi  - Parse an output file name
  33.   cmfld  - Parse an arbitrary field
  34.   cmtxt  - Parse a text string
  35.   cmcfm  - Parse command confirmation (end of line)
  36.   stripq - Strip out backslash quotes from a string.
  37.  
  38.  Return codes:
  39.   -3: no input provided when required
  40.   -2: input was invalid
  41.   -1: reparse required (user deleted into a preceding field)
  42.    0 or greater: success
  43.   See individual functions for greater detail.
  44.  
  45.  Before using these routines, the caller should #include ckucmd.h, and
  46.  set the program's prompt by calling cmsetp().  If the file parsing
  47.  functions cmifi and cmofi are to be used, this module must be linked
  48.  with a ck?fio file system support module for the appropriate system,
  49.  e.g. ckufio for Unix.  If the caller puts the terminal in
  50.  character wakeup ("cbreak") mode with no echo, then these functions will
  51.  provide line editing -- character, word, and line deletion, as well as
  52.  keyword and filename completion upon ESC and help strings, keyword, or
  53.  file menus upon '?'.  If the caller puts the terminal into character
  54.  wakeup/noecho mode, care should be taken to restore it before exit from
  55.  or interruption of the program.  If the character wakeup mode is not
  56.  set, the system's own line editor may be used.
  57.  
  58.  Author: Frank da Cruz (SY.FDC@CU20B),
  59.  Columbia University Center for Computing Activities, January 1985.
  60.  Copyright (C) 1985, Trustees of Columbia University in the City of New York.
  61.  Permission is granted to any individual or institution to use, copy, or
  62.  redistribute this software so long as it is not sold for profit, provided this
  63.  copyright notice is retained. 
  64. */
  65.  
  66. /* Includes */
  67.  
  68. #include <stdio.h>                      /* Standard C I/O package */
  69. #include <ctype.h>                      /* Character types */
  70. #include "ckucmd.h"                     /* Command parsing definitions */
  71. #include "ckcdeb.h"                     /* Formats for debug() */
  72.  
  73. /* Local variables */
  74.  
  75. int psetf = 0,                          /* Flag that prompt has been set */
  76.     cc = 0,                             /* Character count */
  77.     dpx = 0;                            /* Duplex (0 = full) */
  78.  
  79. int hw = HLPLW,                         /* Help line width */
  80.     hc = HLPCW,                         /* Help line column width */
  81.     hh,                                 /* Current help column number */
  82.     hx;                                 /* Current help line position */
  83.  
  84. #define PROML 60                        /* Maximum length for prompt */
  85.  
  86. char cmprom[PROML+1];                   /* Program's prompt */
  87. char *dfprom = "Command? ";             /* Default prompt */
  88.  
  89. char cmerrp[PROML+1];                   /* Program's error message prefix */
  90.  
  91. int cmflgs;                             /* Command flags */
  92.  
  93. char cmdbuf[CMDBL+4];                   /* Command buffer */
  94. char hlpbuf[HLPBL+4];                   /* Help string buffer */
  95. char atmbuf[ATMBL+4];                   /* Atom buffer */
  96. char filbuf[ATMBL+4];                   /* File name buffer */
  97.  
  98. /* Command buffer pointers */
  99.  
  100. static char *bp,                        /* Current command buffer position */
  101.     *pp,                                /* Start of current field */
  102.     *np;                                /* Start of next field */
  103.  
  104. long zchki();                           /* From ck?fio.c. */
  105.  
  106.  
  107. /*  C M S E T P  --  Set the program prompt.  */
  108.  
  109. cmsetp(s) char *s; {
  110.     char *sx, *sy, *strncpy();
  111.     psetf = 1;                          /* Flag that prompt has been set. */
  112.     strncpy(cmprom,s,PROML - 1);        /* Copy the string. */
  113.     cmprom[PROML] = NUL;                /* Ensure null terminator. */
  114.     sx = cmprom; sy = cmerrp;           /* Also use as error message prefix. */
  115.     while (*sy++ = *sx++) ;             /* Copy. */
  116.     sy -= 2; if (*sy == '>') *sy = NUL; /* Delete any final '>'. */
  117. }
  118. /*  C M S A V P  --  Save a copy of the current prompt.  */
  119.  
  120. cmsavp(s,n) int n; char s[]; {
  121.     extern char *strncpy();                                     /* +1   */
  122.     strncpy(s,cmprom,n-1);
  123.     s[n] = NUL;
  124. }
  125.  
  126. /*  P R O M P T  --  Issue the program prompt.  */
  127.  
  128. prompt() {
  129.     if (psetf == 0) cmsetp(dfprom);     /* If no prompt set, set default. */
  130.     printf("\r%s",cmprom);              /* Print the prompt. */
  131. }
  132.  
  133.  
  134. /*  C M R E S  --  Reset pointers to beginning of command buffer.  */
  135.  
  136. cmres() {  
  137.     cc = 0;                             /* Reset character counter. */
  138.     pp = np = bp = cmdbuf;              /* Point to command buffer. */
  139.     cmflgs = -5;                        /* Parse not yet started. */
  140. }
  141.  
  142.  
  143. /*  C M I N I  --  Clear the command and atom buffers, reset pointers.  */
  144.  
  145. /*
  146. The argument specifies who is to echo the user's typein --
  147.   1 means the cmd package echoes
  148.   0 somebody else (system, front end, terminal) echoes
  149. */
  150. cmini(d) int d; {
  151.     for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  152.     *atmbuf = NUL;
  153.     dpx = d;
  154.     cmres();
  155. }
  156.  
  157. stripq(s) char *s; {                    /* Function to strip '\' quotes */
  158.     char *t;
  159.     while (*s) {
  160.         if (*s == '\\') {
  161.             for (t = s; *t != '\0'; t++) *t = *(t+1);
  162.         }
  163.         s++;
  164.     }
  165. }
  166.  
  167.  
  168. /*  C M N U M  --  Parse a number in the indicated radix  */
  169.  
  170. /*  For now, only works for positive numbers in base 10.  */
  171.  
  172. /*
  173.  Returns
  174.    -3 if no input present when required,
  175.    -2 if user typed an illegal number,
  176.    -1 if reparse needed,
  177.     0 otherwise, with n set to number that was parsed
  178. */
  179. cmnum(xhlp,xdef,radix,n) char *xhlp, *xdef; int radix, *n; {
  180.     int x; char *s;
  181.  
  182.     if (radix != 10) {                  /* Just do base 10 for now */
  183.         printf("cmnum: illegal radix - %d\n",radix);
  184.         return(-1);
  185.     }
  186.  
  187.     x = cmfld(xhlp,xdef,&s);
  188.     debug(F101,"cmnum: cmfld","",x);
  189.     if (x < 0) return(x);    /* Parse a field */
  190.  
  191.     if (digits(atmbuf)) {               /* Convert to number */
  192.         *n = atoi(atmbuf);
  193.         return(x);
  194.     } else {
  195.         printf("\n?not a number - %s\n",s);
  196.         return(-2);     
  197.     }
  198. }
  199.  
  200.  
  201. /*  C M O F I  --  Parse the name of an output file  */
  202.  
  203. /*
  204.  Depends on the external function zchko(); if zchko() not available, use
  205.  cmfld() to parse output file names.
  206.  
  207.  Returns
  208.    -3 if no input present when required,
  209.    -2 if permission would be denied to create the file,
  210.    -1 if reparse needed,
  211.     0 or 1 otherwise, with xp pointing to name.
  212. */
  213. cmofi(xhlp,xdef,xp) char *xhlp, *xdef, **xp; {
  214.     int x; char *s;
  215.  
  216.     if (*xhlp == NUL) xhlp = "Output file";
  217.     *xp = "";
  218.  
  219.     if ((x = cmfld(xhlp,xdef,&s)) < 0) return(x);
  220.  
  221.     if (chkwld(s)) {
  222.         printf("\n?Wildcards not allowed - %s\n",s);
  223.         return(-2);
  224.     }
  225.     if (zchko(s) < 0) {
  226.         printf("\n?Write permission denied - %s\n",s);
  227.         return(-2);
  228.     } else {
  229.         *xp = s;
  230.         return(x);
  231.     }
  232. }
  233.  
  234.  
  235. /*  C M I F I  --  Parse the name of an existing file  */
  236.  
  237. /*
  238.  This function depends on the external functions:
  239.    zchki()  - Check if input file exists and is readable.
  240.    zxpand() - Expa